<template>
    <div class="header">
        <div class="header-container">
            <div class="app-name">
                <router-link to="/index">
                    <img src="../../assets/logo.png" style="width: 40px;position: relative; top: 13px;right: 6px">
                    <b style="color: #409EFF" >
                        校园跳蚤市场
                    </b>
                </router-link>
            </div>
            <div class="search-container">
                <el-input placeholder="搜物品..." v-model="searchValue" @keyup.enter.native="searchIdle">
                    <el-button slot="append" icon="el-icon-search" @click="searchIdle"></el-button>
                </el-input>
            </div>
            <el-button type="primary" icon="el-icon-plus"  @click="toRelease">物品发布</el-button>
            <el-button type="primary" icon="el-icon-chat-dot-round" @click="toMessage">消息</el-button>
            <router-link v-if="!isLogin" class="user-name-text" to="/me">个人空间</router-link>
            <el-dropdown trigger="click" v-else append-to-body>
                <div style="cursor:pointer;display: flex;align-items: center;">
                    <div style="font-size: 16px;color: #409EFF;padding-right: 5px;">{{nickname}}</div>
                    <el-avatar :src="avatar"></el-avatar>
                </div>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item @click="toMe">个人空间</el-dropdown-item>
                        <el-dropdown-item divided style="color: red;" @click="loginOut">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>
    </div>
</template>
<script>

    export default {
        name: 'Header',
        props: ['searchInput'],
        data() {
            return {
                searchValue: this.searchInput,
                nickname:'登录',
                avatar:'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
                isLogin:false
            };
        },
        created(){
            this.refreshUserInfo();
        },
        watch: {
            $route() {
                this.refreshUserInfo();
            }
        },
        methods: {
            refreshUserInfo() {
                const token = localStorage.getItem('token');
                this.isLogin = !!token;
                if (token) {
                    const userInfo = localStorage.getItem('userInfo');
                    if (userInfo) {
                        try {
                            const info = JSON.parse(userInfo);
                            this.nickname = info.nickname || info.username || '用户';
                            this.avatar = info.avatar || this.avatar;
                        } catch(e) {
                            this.nickname = '用户';
                            this.avatar = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png';
                        }
                    } else {
                        this.nickname = '用户';
                        this.avatar = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png';
                    }
                } else {
                    this.nickname = '登录';
                    this.avatar = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png';
                }
            },
            searchIdle() {
                if ('/search' !== this.$route.path) {
                    this.$router.push({path: '/search', query: {searchValue: this.searchValue}});
                } else {
                    this.$router.replace({path: '/search', query: {searchValue: this.searchValue}});
                    this.$router.go(0);
                }

            },
            toMe() {
                if ('/me' !== this.$route.path) {
                    this.$router.push({path: '/me'});
                }
            },
            toMessage(){
                if ('/message' !== this.$route.path) {
                    this.$router.push({path: '/message'});
                }
            },
            toRelease(){
                if ('/release' !== this.$route.path) {
                    this.$router.push({path: '/release'});
                }
            },
            toSecondHand() {
                if ('/main/second-hand' !== this.$route.path) {
                    this.$router.push({path: '/main/second-hand'});
                }
            },
            loginOut(){
                this.$api.logout().then(res=>{
                    if(res.status_code===1){
                        localStorage.removeItem('token');
                        localStorage.removeItem('userInfo');
                        if ('/index' === this.$route.path) {
                            this.$router.go(0);
                        }else {
                            this.$router.push({path: '/index'});
                        }
                    }else {
                        this.$message.error('网络或系统异常，退出登录失败！');
                    }
                }).catch(() => {
                    // 即使接口失败也强制本地退出
                    localStorage.removeItem('token');
                    localStorage.removeItem('userInfo');
                    this.$router.push({path: '/index'});
                });
            }
        }
    };
</script>
<style scoped>
    .header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: 58px;
        background: #ffffff;
        display: flex;
        justify-content: center;
        border-bottom: #eeeeee solid 2px;
        z-index: 1000;
    }

    .header-container {
        width: 1000px;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        overflow: visible;
    }

    .app-name a {
        color: #409EFF;
        font-size: 24px;
        text-decoration: none;
    }

    .search-container {
        width: 300px;
    }
    .user-name-text{
        font-size: 16px;
        font-weight: 600;
        color: #409EFF;
        cursor: pointer;
        text-decoration: none;
    }
    :deep(.el-dropdown-menu) {
        z-index: 3000 !important;
        margin-top: 8px !important;
        box-shadow: 0 4px 16px rgba(0,0,0,0.10);
    }
</style>
